package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.dto.e.DataAnalysisKeyEntDTO;
import com.macro.mall.dto.o.DataAnalysisKeyOutDTO;
import com.macro.mall.mapper.BasicKeyTextMapper;
import com.macro.mall.mapper.DpsTagKeyMapper;
import com.macro.mall.model.BasicKeyText;
import com.macro.mall.model.DpsTagKey;
import com.macro.mall.service.DataAnalysisKeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DataAnalysisKeyServiceImpl implements DataAnalysisKeyService {
    @Autowired
    private DpsTagKeyMapper dpsTagKeyMapper;
    @Autowired
    private BasicKeyTextMapper basicKeyTextMapper;
    @Override
    public PageInfo<DataAnalysisKeyOutDTO> list(DataAnalysisKeyEntDTO dataAnalysisKeyEntDTO) {
        PageHelper.startPage(dataAnalysisKeyEntDTO.getPage(),dataAnalysisKeyEntDTO.getPageSize());
        List<DpsTagKey> list = dpsTagKeyMapper.list(dataAnalysisKeyEntDTO.getLabel());
        PageInfo<DpsTagKey> dpsTagKeyPageInfo = new PageInfo<>(list);
        List<DataAnalysisKeyOutDTO> list1=new ArrayList<>();
        for (DpsTagKey dpsTagKey:list){
            DataAnalysisKeyOutDTO dataAnalysisKeyOutDTO = new DataAnalysisKeyOutDTO();
            dataAnalysisKeyOutDTO.setId(dpsTagKey.getId());
            dataAnalysisKeyOutDTO.setCreatedDt(dpsTagKey.getCreatedDt());
            dataAnalysisKeyOutDTO.setKey(dpsTagKey.getKeyText());
            dataAnalysisKeyOutDTO.setLabel(dpsTagKey.getKeyType());
            dataAnalysisKeyOutDTO.setStatus(dpsTagKey.getStatus());
            list1.add(dataAnalysisKeyOutDTO);
        }
        PageInfo<DataAnalysisKeyOutDTO> dataAnalysisKeyOutDTOPageInfo = new PageInfo<>(list1);
        dataAnalysisKeyOutDTOPageInfo.setTotal(dpsTagKeyPageInfo.getTotal());
        return dataAnalysisKeyOutDTOPageInfo;
    }

    @Override
    public int modify(DataAnalysisKeyEntDTO dataAnalysisKeyEntDTO) {
        DpsTagKey dpsTagKey1 = dpsTagKeyMapper.selectByPrimaryKey(dataAnalysisKeyEntDTO.getId());
        if (dataAnalysisKeyEntDTO.getStatus() !=null && dataAnalysisKeyEntDTO.getStatus() !=""){
            DpsTagKey dpsTagKey = new DpsTagKey();
            dpsTagKey.setId(dataAnalysisKeyEntDTO.getId());
            dpsTagKey.setStatus(dataAnalysisKeyEntDTO.getStatus());
            return dpsTagKeyMapper.updateByPrimaryKeySelective(dpsTagKey);
        }
        if (dataAnalysisKeyEntDTO.getLabel() !=null && dataAnalysisKeyEntDTO.getLabel() !=""){
            DpsTagKey dpsTagKey = new DpsTagKey();
            dpsTagKey.setId(dataAnalysisKeyEntDTO.getId());
            dpsTagKey.setKeyType(dataAnalysisKeyEntDTO.getLabel());
            return dpsTagKeyMapper.updateByPrimaryKeySelective(dpsTagKey);
        }
        if (dataAnalysisKeyEntDTO.getBasicKey()!=null && dataAnalysisKeyEntDTO.getBasicKey()!=""){
            BasicKeyText basicKeyText1 = new BasicKeyText();
            basicKeyText1.setId(dpsTagKey1.getId());
            basicKeyText1.setKeyText(dataAnalysisKeyEntDTO.getBasicKey());
            return basicKeyTextMapper.updateByPrimaryKeySelective(basicKeyText1);
        }

        return 0;
    }

}
